//
//  ExtendSelector.m
//  etionUI
//
//  Created by wangjian on 12/10/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "ExtendSelector.h"
#import "CircleButton.h"

#define ICON_SIZE 32
#define ICON_MARGIN 10
#define ICON_LOC_OFFSET 20

/**
 *  CExtendColorSelector
 */

@interface CExtendColorSelector()

@property (nonatomic, assign) UIView *anchorView;
@property (nonatomic, retain) NSArray *arColorBtns;
@property (nonatomic, retain) NSArray *arWidthBtns;
@property (nonatomic, retain) CCircleColorButton *baseBtn;

@end

@implementation CExtendColorSelector

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.baseBtn = [[[CCircleColorButton alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, ICON_SIZE, ICON_SIZE)] autorelease];
        [self.baseBtn addTarget:self action:@selector(baseBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _bIsExtend = NO;
    }
    return self;
}

- (void)dealloc
{
    [_arColors release];
    [_arColorBtns release];
    [_arWidths release];
    [_arWidthBtns release];
    [_baseBtn release];
    [super dealloc];
}

- (void)didMoveToSuperview
{
    if (self.superview != nil)
    {
        [self.superview addSubview:self.baseBtn];
    }
    else
    {
        [self.baseBtn removeFromSuperview];
    }
}

- (void)setArColors:(NSArray *)arColors
{
//    for (CCircleColorButton *btn in _arColorBtns)
//    {
//        [btn removeFromSuperview];
//    }
    
    NSArray *tmp = [arColors retain];
    [_arColors release];
    _arColors = tmp;
    
    NSMutableArray *arBtn = [NSMutableArray array];
    for (UIColor *color in arColors)
    {
        CCircleColorButton *btn = [[[CCircleColorButton alloc] initWithFrame:self.baseBtn.frame] autorelease];
        [btn setColor:color animated:NO];
//        btn.alpha = 0.0;
        [arBtn addObject:btn];
        [btn addTarget:self action:@selector(colorBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
//        [self.anchorView insertSubview:btn belowSubview:self.baseBtn];
    }
    self.arColorBtns = arBtn;
    [self setCurColorIndex:self.curColorIndex animated:NO];
}

- (void)setArWidths:(NSArray *)arWidths
{
//    for (CCircleColorButton *btn in _arWidthBtns)
//    {
//        [btn removeFromSuperview];
//    }
    
    NSArray *tmp = [arWidths retain];
    [_arWidths release];
    _arWidths = tmp;
    
    NSMutableArray *arBtn = [NSMutableArray array];
    for (NSNumber *numWidth in arWidths)
    {
        CCircleColorButton *btn = [[[CCircleColorButton alloc] initWithFrame:self.baseBtn.frame] autorelease];
        [btn setRadius:[numWidth floatValue]/2 animated:NO];
        btn.alpha = 0.0;
        if (self.arColors.count > 0)
        {
            [btn setColor:[self.arColors objectAtIndex:self.curColorIndex] animated:NO];
        }
        [arBtn addObject:btn];
        [btn addTarget:self action:@selector(sizeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
//        [self.anchorView insertSubview:btn belowSubview:self.baseBtn];
    }
    self.arWidthBtns = arBtn;
    [self setCurWidthIndex:self.curWidthIndex animated:NO];
}

- (void)setCurColorIndex:(NSUInteger)curColorIndex
{
    [self setCurColorIndex:curColorIndex animated:NO];
}

- (void)setCurWidthIndex:(NSUInteger)curWidthIndex
{
    [self setCurWidthIndex:curWidthIndex animated:NO];
}

- (void)setCurColorIndex:(NSUInteger)curColorIndex animated:(BOOL)animated
{
    _curColorIndex = curColorIndex;
    [self.baseBtn setColor:[self.arColors objectAtIndex:curColorIndex] animated:animated];
    for (CCircleColorButton *btn in self.arWidthBtns)
    {
        [btn setColor:[self.arColors objectAtIndex:curColorIndex] animated:animated];
    }
}

- (void)setCurWidthIndex:(NSUInteger)curWidthIndex animated:(BOOL)animated
{
    _curWidthIndex = curWidthIndex;
    [self.baseBtn setRadius:[[self.arWidths objectAtIndex:curWidthIndex] floatValue]/2 animated:animated];
}

- (UIColor *)curColor
{
    if (self.curColorIndex < self.arColors.count)
    {
        return [self.arColors objectAtIndex:self.curColorIndex];
    }
    return nil;
}

- (CGFloat)curWidth
{
    if (self.curWidthIndex < self.arWidths.count)
    {
        return [[self.arWidths objectAtIndex:self.curWidthIndex] floatValue];
    }
    return 0.0;
}

- (void)setFrame:(CGRect)frame
{
    CGPoint offset = CGPointMake(frame.origin.x-self.frame.origin.x, frame.origin.y-self.frame.origin.y);
    [super setFrame:frame];
    self.baseBtn.frame = CGRectOffset(self.baseBtn.frame, offset.x, offset.y);
    for (CCircleColorButton *btn in self.arWidthBtns)
    {
        btn.frame = CGRectOffset(btn.frame, offset.x, offset.y);
    }
    for (CCircleColorButton *btn in self.arColorBtns)
    {
        btn.frame = CGRectOffset(btn.frame, offset.x, offset.y);
    }
}

- (void)setAlpha:(CGFloat)alpha
{
    [super setAlpha:alpha];
    self.baseBtn.alpha = alpha;
}

- (void)setHidden:(BOOL)hidden
{
    [super setHidden:hidden];
    self.baseBtn.hidden = hidden;
}

- (void)extend
{
    if (self.bIsExtend)
    {
        return;
    }
    
    _bIsExtend = YES;
    [UIView animateWithDuration:.25 animations:^{
        for (NSUInteger i=0; self.arColorBtns.count > 1 && i<self.arColorBtns.count; i++)
        {
            CCircleColorButton *btn = [self.arColorBtns objectAtIndex:i];
            [self.superview addSubview:btn];
            btn.frame = CGRectMake(self.baseBtn.left, self.baseBtn.top-(i+1)*(ICON_SIZE+ICON_MARGIN), ICON_SIZE, ICON_SIZE);
            btn.alpha = 1.0;
        }
        for (NSUInteger i=0; self.arWidthBtns.count > 1 && i<self.arWidthBtns.count; i++)
        {
            CCircleColorButton *btn = [self.arWidthBtns objectAtIndex:i];
            [self.superview addSubview:btn];
            btn.frame = CGRectMake(self.baseBtn.left+(i+1)*(ICON_SIZE+ICON_MARGIN), self.baseBtn.top, ICON_SIZE, ICON_SIZE);
            btn.alpha = 1.0;
        }
    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)shrink
{
    if (!self.bIsExtend)
    {
        return;
    }
    
    _bIsExtend = NO;
    [UIView animateWithDuration:.25 animations:^{
        for (NSUInteger i=0; self.arColorBtns.count > 1 && i<self.arColorBtns.count; i++)
        {
            CCircleColorButton *btn = [self.arColorBtns objectAtIndex:i];
            btn.frame = self.baseBtn.frame;
            btn.alpha = 0.0;
        }
        for (NSUInteger i=0; self.arWidthBtns.count > 1 && i<self.arWidthBtns.count; i++)
        {
            CCircleColorButton *btn = [self.arWidthBtns objectAtIndex:i];
            btn.frame = self.baseBtn.frame;
            btn.alpha = 0.0;
        }
    } completion:^(BOOL finished) {
        for (CCircleColorButton *btn in self.arWidthBtns)
        {
            [btn removeFromSuperview];
        }
        for (CCircleColorButton *btn in self.arColorBtns)
        {
            [btn removeFromSuperview];
        }
    }];
}

- (void)baseBtnClicked:(CCircleColorButton *)baseBtn
{
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    if (self.bIsExtend == NO)
    {
        [self extend];
    }
    else
    {
        [self shrink];
    }
}

- (void)colorBtnClicked:(CCircleColorButton *)btn
{
    NSUInteger index = [self.arColorBtns indexOfObject:btn];
    if (index != self.curColorIndex)
    {
        [self setCurColorIndex:index animated:YES];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    [self shrink];
}

- (void)sizeBtnClicked:(CCircleColorButton *)btn
{
    NSUInteger index = [self.arWidthBtns indexOfObject:btn];
    if (index != self.curWidthIndex)
    {
        [self setCurWidthIndex:index animated:YES];
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
    [self shrink];
}

@end


@interface CExtendItemSelector()

@property (nonatomic, retain) NSMutableArray *arItems;
@property (nonatomic, assign) CGFloat iconH;

@end

@implementation CExtendItemSelector

- (id)initWithFrame:(CGRect)frame normalImages:(NSArray *)normalImages highlightedImages:(NSArray *)highlightedImages titles:(NSArray *)titles
{
    if (self = [super initWithFrame:frame])
    {
        self.arItems = [NSMutableArray array];
        self.iconH = self.height;
        
        [self setItemsWithNormalImages:normalImages highlightedImages:highlightedImages titles:titles];
    }
    return self;
}

- (void)dealloc
{
    [_arItems release];
    [super dealloc];
}

- (void)setFrame:(CGRect)frame
{
    CGPoint offset = CGPointMake(frame.origin.x-self.frame.origin.x, frame.origin.y-self.frame.origin.y);
    [super setFrame:frame];
    for (CCircleButton *item in self.arItems)
    {
        item.frame = CGRectOffset(item.frame, offset.x, offset.y);
    }
}

- (void)didMoveToSuperview
{
    if (self.superview != nil)
    {
        CCircleButton *item = [self.arItems objectAtIndex:self.curSelectedIndex];
        [self.superview addSubview:item];
    }
    else
    {
        for (CCircleButton *item in self.arItems)
        {
            [item removeFromSuperview];
        }
    }
}

- (CGRect)frameOfItemAtIndex:(NSUInteger)index withExtendState:(BOOL)isExtend
{
    CGRect frame = CGRectZero;
    CCircleButton *item = [self.arItems objectAtIndex:index];
    frame.size = item.size;
    frame.origin.x = CGRectGetMaxX(self.frame)-item.width;
    if (isExtend == NO || index == self.curSelectedIndex)
    {
        frame.origin.y = CGRectGetMaxY(self.frame)-item.height;
    }
    else
    {
        NSUInteger dspIndex = index > self.curSelectedIndex ? index : index + 1;
        frame.origin.y = CGRectGetMinY(self.frame)+(dspIndex*(self.iconH+ICON_MARGIN))*(self.extendDir == EExtendSelectorDirUp ? -1 : 1);
    }
    return frame;
}

- (void)setCurSelectedIndex:(NSUInteger)curSelectedIndex
{
    if (self.arItems.count > 0)
    {
        [[self.arItems objectAtIndex:_curSelectedIndex] removeFromSuperview];
        [self.superview addSubview:[self.arItems objectAtIndex:curSelectedIndex]];
    }
    
    _curSelectedIndex = curSelectedIndex;
}

- (void)setItemsWithNormalImages:(NSArray *)normalImages highlightedImages:(NSArray *)highlightedImages titles:(NSArray *)titles
{
    for (CCircleButton *item in self.arItems)
    {
        [item removeFromSuperview];
    }
    [self.arItems removeAllObjects];
        
    for (NSUInteger i=0; i<normalImages.count; i++)
    {
        CCircleButton *item = [[[CCircleButton alloc] initWithNormalImage:[normalImages objectAtIndex:i] highlightedImage:i<highlightedImages.count?[highlightedImages objectAtIndex:i]:nil radius:ICON_SIZE/2 title:i<titles.count?[titles objectAtIndex:i]:nil titlePosition:ECircleButtonTitlePositionLeft] autorelease];
        [item addTarget:self action:@selector(extendSelectorItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.arItems addObject:item];
        [item setTitleState:NO animated:NO];
        item.frame = [self frameOfItemAtIndex:i withExtendState:NO];
    }
    [self setCurSelectedIndex:self.curSelectedIndex];
}

- (void)extend
{
    if (self.bIsExtend)
    {
        return;
    }
    
    for (NSUInteger i=0; i<self.arItems.count; i++)
    {
        if (i != self.curSelectedIndex)
        {
            CCircleButton *item = [self.arItems objectAtIndex:i];
            [self.superview addSubview:item];
            item.alpha = 0.0;
        }
    }
    [self.superview bringSubviewToFront:[self.arItems objectAtIndex:self.curSelectedIndex]];
    
    _bIsExtend = YES;
    [UIView animateWithDuration:.25 animations:^{
        for (NSUInteger i=0; i<self.arItems.count; i++)
        {
//            if (i != self.curSelectedIndex)
            {
                CCircleButton *item = [self.arItems objectAtIndex:i];
                item.frame = [self frameOfItemAtIndex:i withExtendState:YES];
                item.alpha = 1.0;
                [item setTitleState:YES animated:YES];
            }
        }
    } completion:^(BOOL finished) {
    }];
}

- (void)shrink
{
    if (!self.bIsExtend)
    {
        return;
    }
    
    _bIsExtend = NO;
    [self bringSubviewToFront:[self.arItems objectAtIndex:self.curSelectedIndex]];
    [UIView animateWithDuration:.25 animations:^{
        for (NSUInteger i=0; i<self.arItems.count; i++)
        {
            CCircleButton *item = [self.arItems objectAtIndex:i];
            item.frame = [self frameOfItemAtIndex:i withExtendState:self.bIsExtend];
            [item setTitleState:NO animated:YES];
            if (self.curSelectedIndex != i)
            {
                item.alpha = 0.0;
            }
        }
    } completion:^(BOOL finished) {
        for (NSUInteger i=0; i<self.arItems.count; i++)
        {
            if (self.curSelectedIndex != i)
            {
                [[self.arItems objectAtIndex:i] removeFromSuperview];
            }
        }
    }];
}

- (void)shrinkBtnClicked:(id)sender
{
    [self shrink];
}

- (void)extendSelectorItemClicked:(CCircleButton *)item
{
    if (self.bIsExtend == NO)
    {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
        [self extend];
    }
    else
    {
        NSUInteger index = [self.arItems indexOfObject:item];
        if (index != self.curSelectedIndex)
        {
            _curSelectedIndex = index;
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
        [self shrink];
    }
}

@end
